

####################################################################################################################
#
#   Replication file for "Parties’ voter targeting strategies. What can Facebook ads tell us?" 
#
#   published in Electoral Studies 
#   
#   by Simon Stuckelberger and Jelle Koedam 
#
####################################################################################################################



rm(list=ls())

install.packages("pacman")

#install not yet installed packages and load all packages 
pacman::p_load(dplyr, ggplot2, cowplot, grid, gridExtra, ggpubr)

# Figure 1 ----------------------------------------------------------------

load("replication_data_Figure1.Rdata")

win.metafile("Figure1.wmf")
data_Figure1 %>% 
  ggplot(data=., aes(x=PartyFamily,y=AudienceShare)) +
  geom_boxplot()+
  labs(x="",y="Share of ad audience")+
  theme(axis.text.x = element_text(angle=45,size=9,hjust=1,vjust=1))+
  facet_grid(.~group,scales="free",space="free")+scale_y_continuous(breaks=seq(0,0.8,0.1),limits=c(0,0.8))+
  theme(axis.text.x = element_text(size=11,vjust=1,color = "black"),
        axis.text.y = element_text(size=11,color = "black"),
        axis.title.y=element_text(size=11,vjust=3,color = "black"),
        strip.text=element_text(size=14,color = "black"))+
  theme(text=element_text(family="serif"))
dev.off()



# Figure A1 ---------------------------------------------------------------

load("replication_data_FigureA1.Rdata")


#plot
win.metafile("FigureA1.wmf")
data_FigureA1 %>% 
  ggplot(data=., aes(x=PartyFamily,y=AudienceShare_women)) +
  geom_boxplot()+
  labs(x="",y="Female ad audience share")+
  theme(axis.text.x = element_text(angle=45,size=9,hjust=1,vjust=1))+
  facet_grid(.~group,scales="free",space="free")+scale_y_continuous(breaks=seq(0,0.8,0.1),limits=c(0,0.8))+
  theme(axis.text.x = element_text(size=11,vjust=1,color = "black"),
        axis.text.y = element_text(size=11,color = "black"),
        axis.title.y=element_text(size=11,vjust=3,color = "black"),
        strip.text=element_text(size=14,color = "black"))+
  theme(text=element_text(family="serif"))
dev.off()




# Figure 2 ----------------------------------------------------------------

load("replication_data_Figure2.Rdata")

#Create separate plots for women, men, younger people, older people 
plot_women<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_women", y = "AudienceShare_women",
            label="party",
            add = "reg.line",  
            add.params = list(color = "black", fill = "lightgray"))
plot_women<-plot_women + stat_cor(method = "pearson",p.accuracy = 0.001)+ scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) + ggtitle("Women")+
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of women (standardised)",y="Female ad audience share (standardised)")+
  theme(text=element_text(family="serif"))
plot_women


plot_men<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_men", y = "AudienceShare_men",
            label="party",
            add = "reg.line",  
            add.params = list(color = "black", fill = "lightgray"))
plot_men<-plot_men + stat_cor(method = "pearson",p.accuracy = 0.001)+ scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) + ggtitle("Men")+
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of men (standardised)",y="Male ad audience share (standardised)")+
  theme(text=element_text(family="serif"))
plot_men

plot_younger<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_young", y = "AudienceShare_young",
            label="party",
            add = "reg.line",  
            add.params = list(color = "black", fill = "lightgray"))
plot_younger<-plot_younger + stat_cor(method = "pearson",p.accuracy = 0.001) + scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +ggtitle("Younger People")+
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of younger people (standardised)",y="Ad audience share of younger people (standardised)")+
  theme(text=element_text(family="serif"))
plot_younger

plot_older<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_older", y = "AudienceShare_older",
            label="party",
            add = "reg.line",  
            add.params = list(color = "black", fill = "lightgray"))
plot_older<-plot_older + stat_cor(method = "pearson",p.accuracy = 0.001) + scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +ggtitle("Older People")+
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of older people (standardised)",y="Ad audience share of older people (standardised)")+
  theme(text=element_text(family="serif"))
plot_older


##Combine plots to Figure 2 
win.metafile("Figure2.wmf",width=15,height=12)
grid.arrange(plot_women,plot_men,plot_younger,plot_older)
dev.off()

# Figures A2 - A5 --------------------------------------------------------

data_Figure2$country<-plyr::mapvalues(data_Figure2$country,from=c("CA","UK","AT","CH","IE"),
                               to=c("Canada","United Kingdom","Austria","Switzerland","Ireland"))

plot_women_country<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_women", y = "AudienceShare_women",
            label="party",
            add = "reg.line",
            facet.by="country",
            add.params = list(color = "black", fill = "lightgray"))
plot_women_country<-plot_women_country + stat_cor(method = "pearson")+ scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of women (standardised)",y="Female ad audience share (standardised)")+
  theme(text=element_text(family="serif"))

win.metafile("FigureA2.wmf")
plot_women_country
dev.off()


plot_men_country<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_men", y = "AudienceShare_men",
            label="party",
            add = "reg.line",  
            facet.by="country",
            add.params = list(color = "black", fill = "lightgray"))
plot_men_country<-plot_men_country + stat_cor(method = "pearson")+ scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) + 
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of men (standardised)",y="Male ad audience share (standardised)")+
  theme(text=element_text(family="serif"))

win.metafile("FigureA3.wmf")
plot_men_country
dev.off()


plot_younger_country<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_young", y = "AudienceShare_young",
            label="party",
            add = "reg.line",  
            facet.by="country",
            add.params = list(color = "black", fill = "lightgray"))
plot_younger_country<-plot_younger_country + stat_cor(method = "pearson") + scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of younger people (standardised)",y="Ad audience share of younger people (standardised)")+
  theme(text=element_text(family="serif"))

win.metafile("FigureA4.wmf")
plot_younger_country
dev.off()


plot_older_country<-data_Figure2 %>% 
  ggscatter(., x = "VoteShare_older", y = "AudienceShare_older",
            label="party",
            add = "reg.line",  
            facet.by="country",
            add.params = list(color = "black", fill = "lightgray"))
plot_older_country<-plot_older_country+ stat_cor(method = "pearson") + scale_y_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  scale_x_continuous(breaks=seq(-2,2,1),limits=c(-3,3)) +
  geom_vline(xintercept = 0)+
  geom_hline(yintercept=0)+
  labs(x="Vote share of older people (standardised)",y="Ad audience share of older people (standardised)")+
  theme(text=element_text(family="serif"))

win.metafile("FigureA5.wmf")
plot_older_country
dev.off()



# Figure 3 ----------------------------------------------------------------

load("replication_data_Figure3.Rdata")

#Plot separate figures for significant and non-significant correlations
plot_sig<-data_Figure3 %>%
  filter(p_value<0.05) %>% 
  ggplot(aes(x = VoteShare, y = AudienceShare_Region_popShare)) +
  geom_point() +
  geom_smooth(formula = y ~ x,method = "lm",color="black",se=FALSE)+
  facet_wrap( ~party_label,scales="free",ncol=6)+
  theme(aspect.ratio=1)+
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))+
  theme(legend.position="none",axis.title.x=element_blank(),axis.title.y=element_blank())+
  theme(text=element_text(family="serif"))
plot_sig

plot_sigN<-data_Figure3 %>%
  filter(p_value>=0.05) %>% 
  ggplot(aes(x = VoteShare, y = AudienceShare_Region_popShare)) +
  geom_point() +
  geom_smooth(formula = y ~ x,method = "lm",color="black",linetype="dashed",se=FALSE)+
  facet_wrap( ~party_label,scales="free",ncol=6)+
  theme(aspect.ratio=1)+ 
  labs(x="Regional vote share",y="Regional ad audience share/regional population share")+
  theme(axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0)),axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)))+
  theme(legend.position="none",axis.title.x=element_blank(),axis.title.y=element_blank())+
  theme(text=element_text(family="serif"))
plot_sigN

#combine both plots 
Figure3 <- plot_grid(plot_sig,NULL,plot_sigN, align = "v", nrow = 3, rel_heights = c(0.36,0.025,0.54))

win.metafile("Figure3.wmf",width=12,height=11)
Figure3
dev.off()




# Figure 4 ----------------------------------------------------------------

load("replication_data_Figure4.Rdata")

win.metafile("Figure4.wmf")
data_Figure4 %>% 
  ggplot(data=., aes(x=country,y=shannon_normalized)) +
  geom_boxplot()+
  facet_grid(.~system,scales="free",space="free")+
  labs(x="",y="Normalised Shannon index of regional concentration")+
  theme(axis.text.x = element_text(size=11,vjust=1,color = "black"),
        axis.text.y = element_text(size=11,color = "black"),
        axis.title.y=element_text(size=11,vjust=3,color = "black"),
        strip.text=element_text(size=14,color = "black"))+
  scale_y_continuous(breaks=seq(0,1,0.1))+theme(text=element_text(family="serif"))
dev.off()
